package GUILayer;

import ModelLayer.*;
import ControlLayer.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class AddToInvoice extends JFrame {

	private JPanel contentPane;
	private JPanel panelProduct;
	private JPanel panelCustomer;
	private JPanel panelDelivery;
	private JPanel panelEmployee;
	
	private JTable tableProduct;
	private JTable tableCustomer;
	private JTable tableDelivery;
	private JTable tableEmployee;

	private ArrayList<Contact> contacts;
	private ArrayList<ModelLayer.Delivery> deliveries;
	private ArrayList<ModelLayer.Employee> employees;
	private ArrayList<ModelLayer.Product> products;
	
	private IFSearch search;
	private IFCreate create;
	private IFUpdate update;
	private JTextField txtQuantity;
	
	private GUILayer.Product product;
	private GUILayer.CustomerDetails customerD;
	private GUILayer.NewCustomer customerN;
	private GUILayer.Delivery delivery;
	private GUILayer.Employee employee;

	/**
	 * Create the frame.
	 */
	public AddToInvoice(String s) {

		ArrayList<Contact> contacts = new ArrayList<Contact>();
		ArrayList<Delivery> deliveries = new ArrayList<Delivery>();
		ArrayList<Employee> employees = new ArrayList<Employee>();
		ArrayList<Product> products = new ArrayList<Product>();
		
		search = new SearchControler();
		create = new CreateControler();
		update = new UpdateControler();
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 690, 490);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		panelProduct = new JPanel();
		panelCustomer = new JPanel();
		panelDelivery = new JPanel();
		panelEmployee = new JPanel();
		
		buildProduct();
		buildCustomer();
		buildDelivery();
		buildEmployee();
		
		
		showSelected(s);
		

	}
	
	private void showSelected(String s)
	{
		switch (s)
		{
			case "product":
				panelProduct.setVisible(true);
				break;
			case  "customer":
				panelCustomer.setVisible(true);
				break;
			case "delivery":
				panelDelivery.setVisible(true);
				break;
			case "employee":
				panelEmployee.setVisible(true);
				break;
		}
	}
	
	private void buildProduct()
	{
		panelProduct.setBackground(new Color(255, 255, 204));
		panelProduct.setBounds(0, 0, 675, 478);
		contentPane.add(panelProduct);
		panelProduct.setLayout(null);
		
		JLabel lblFilter = new JLabel("Filter:");
		lblFilter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFilter.setBounds(0, 0, 39, 17);
		panelProduct.add(lblFilter);
		
		JLabel lblProductName = new JLabel("Name:");
		lblProductName.setBounds(129, 31, 62, 14);
		panelProduct.add(lblProductName);
		
		final JTextField txtProductName = new JTextField();
		txtProductName.setBounds(190, 28, 86, 20);
		panelProduct.add(txtProductName);
		txtProductName.setColumns(10);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 31, 22, 14);
		panelProduct.add(lblId);
		
		final JTextField txtPid = new JTextField();
		txtPid.setBounds(33, 28, 86, 20);
		panelProduct.add(txtPid);
		txtPid.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				products.clear();
				if(!txtPid.getText().isEmpty())
				{	
					products.add(search.getProductById(Integer.parseInt(txtPid.getText())));
				}
				else
				{
					if(!txtProductName.getText().isEmpty())
					{
						products = search.getProductsLikeName(txtProductName.getText());
					}
					else
					{
						products = search.getAllProducts();
					}
				}
				setTableProduct(tableProduct, products);
			}
		});
		btnSearch.setBounds(556, 74, 109, 23);
		panelProduct.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 105, 655, 218);
		panelProduct.add(scrollPane);
		
		products = search.getAllProducts();
		tableProduct = new JTable();
		tableProduct.setFillsViewportHeight(true);
		setTableProduct(tableProduct, products);
	    tableProduct.getColumnModel().getColumn(0).setResizable(false);
	    tableProduct.getColumnModel().getColumn(1).setResizable(false);
		tableProduct.getColumnModel().getColumn(2).setResizable(false);
		tableProduct.getColumnModel().getColumn(3).setResizable(false);
		tableProduct.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableProduct.setBackground(Color.WHITE);
		scrollPane.setColumnHeaderView(tableProduct);
		
		
		JButton btnShow = new JButton("Show details");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selRowIndex = tableProduct.getSelectedRow();
                if(selRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                        int idSelected = (Integer) tableProduct.getValueAt(selRowIndex, 0);
                        		product = new GUILayer.Product(false, false, idSelected);
                        		product.setVisible(true);
                }
			}
		});
		btnShow.setBounds(10, 334, 109, 23);
		panelProduct.add(btnShow);
		
		JButton btnAddNew = new JButton("New");
		btnAddNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				product = new GUILayer.Product(true, false, create.getNextProductId());
				product.setVisible(true);
			}
		});
		btnAddNew.setBounds(129, 334, 109, 23);
		panelProduct.add(btnAddNew);
		
		JLabel lblQuantity = new JLabel("Quantity:");
		lblQuantity.setBounds(337, 338, 77, 14);
		panelProduct.add(lblQuantity);
		
		txtQuantity = new JTextField();
		txtQuantity.setBounds(424, 335, 86, 20);
		panelProduct.add(txtQuantity);
		txtQuantity.setColumns(10);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selRowIndex = tableProduct.getSelectedRow();
				if(selRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                	DataHolder.getInstance().addToSalesLines(new SalesLine(Integer.parseInt(txtQuantity.getText()),search.getProductById(Integer.parseInt(tableProduct.getValueAt(selRowIndex, 0).toString()))));
                	dispose();
                }
			}
		});
		btnAdd.setBounds(556, 334, 109, 23);
		panelProduct.add(btnAdd);
		
		panelProduct.setVisible(false);
	}
	
	private void setTableProduct(JTable jTable, ArrayList<ModelLayer.Product> products) {
	    DefaultTableModel model = new DefaultTableModel();
	    jTable.setModel(model);
	    model.setColumnIdentifiers(new String[]{"ID", "Name", "Stock amount", "Price"});
	    for (ModelLayer.Product p : products) {
	        model.addRow(new String[]{Integer.toString(p.getId()), p.getName(), Integer.toString(p.getStockAmount()), Double.toString(p.getNettPrice())});
	    }
	}
	
	public void refreshTProduct()
	{
		products = search.getAllProducts();
		setTableProduct(tableProduct, products);
	}
	
	private void buildCustomer()
	{
		panelCustomer.setBackground(new Color(255, 255, 204));
		panelCustomer.setBounds(0, 0, 675, 478);
		contentPane.add(panelCustomer);
		panelCustomer.setLayout(null);
		
		JLabel lblFilter = new JLabel("Filter:");
		lblFilter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFilter.setBounds(0, 0, 39, 17);
		panelCustomer.add(lblFilter);
		
		JLabel lblCustomerName = new JLabel("Name:");
		lblCustomerName.setBounds(129, 31, 62, 14);
		panelCustomer.add(lblCustomerName);
		
		final JTextField txtCustomerName = new JTextField();
		txtCustomerName.setBounds(190, 28, 86, 20);
		panelCustomer.add(txtCustomerName);
		txtCustomerName.setColumns(10);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 31, 22, 14);
		panelCustomer.add(lblId);
		
		final JTextField txtCid = new JTextField();
		txtCid.setBounds(33, 28, 86, 20);
		panelCustomer.add(txtCid);
		txtCid.setColumns(10);
		
		JLabel lblPayment = new JLabel("Type:");
		lblPayment.setBounds(10, 56, 109, 14);
		panelCustomer.add(lblPayment);
		
		final JComboBox comboPayment = new JComboBox();
		comboPayment.setModel(new DefaultComboBoxModel(new String[] {"", "Private", "Company"}));
		comboPayment.setBounds(10, 74, 109, 20);
		panelCustomer.add(comboPayment);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				contacts.clear();
				if(!txtCid.getText().isEmpty())
				{	
					contacts.add(search.getCustomerById(Integer.parseInt(txtCid.getText())));
				}
				else
				{
					if(!txtCustomerName.getText().isEmpty())
					{
						if(!comboPayment.getSelectedItem().toString().isEmpty())
						{
							contacts = search.getCustomersByNameAndType(txtCustomerName.getText(), comboPayment.getSelectedItem().toString());
						}
						else
						{
							contacts = search.getCustomersLikeName(txtCustomerName.getText());
						}
					}
					else
					{
						if(!comboPayment.getSelectedItem().toString().isEmpty())
						{
							if(comboPayment.getSelectedItem().toString().equals("Private"))
							{
								contacts = search.getAllPrivateCustomers();
							}
							else
							{
								contacts = search.getAllBusinessCustomers();
							}
						}else
						{
							contacts = search.getAllCustomers();
						}
					}
				}
				setTableCustomer(tableCustomer, contacts);
			}
		});
		btnSearch.setBounds(556, 74, 109, 23);
		panelCustomer.add(btnSearch);
		
		JScrollPane scrollPaneCustomer = new JScrollPane();
		scrollPaneCustomer.setBounds(10, 105, 655, 218);
		panelCustomer.add(scrollPaneCustomer);
		
		contacts = search.getAllCustomers();
		tableCustomer = new JTable();
		tableCustomer.setFillsViewportHeight(true);
		setTableCustomer(tableCustomer, contacts);
		tableCustomer.getColumnModel().getColumn(0).setResizable(false);
		tableCustomer.getColumnModel().getColumn(1).setResizable(false);
		tableCustomer.getColumnModel().getColumn(2).setResizable(false);
		tableCustomer.getColumnModel().getColumn(3).setResizable(false);
		tableCustomer.getColumnModel().getColumn(4).setResizable(false);
		tableCustomer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableCustomer.setBackground(Color.WHITE);
		scrollPaneCustomer.getViewport().add(tableCustomer);
		
		JButton btnShow = new JButton("Show details");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableCustomer.getSelectedRow();
                if(SelRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                        String idSelected = (String) tableCustomer.getValueAt(SelRowIndex, 0);
                        int idSel = Integer.parseInt(idSelected);
                        Contact c = search.getCustomerById(idSel);
                        boolean isPrivate = false;
                        if(c instanceof Private)
                        {
                        	isPrivate=true;
                        }
                        customerD = new GUILayer.CustomerDetails(idSel, isPrivate, false);
                        customerD.setVisible(true);
                }
			}
		});
		btnShow.setBounds(10, 334, 109, 23);
		panelCustomer.add(btnShow);
		
		JButton btnAddNew = new JButton("New");
		btnAddNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				customerN = new NewCustomer();
				customerN.setVisible(true);
			}
		});
		btnAddNew.setBounds(129, 334, 109, 23);
		panelCustomer.add(btnAddNew);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selRowIndex = tableCustomer.getSelectedRow();
				if(selRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                	DataHolder.getInstance().setCustomerHolder(search.getCustomerById(Integer.parseInt(tableCustomer.getValueAt(selRowIndex, 0).toString())));
                	dispose();
                }
			}
		});
		btnAdd.setBounds(556, 334, 109, 23);
		panelCustomer.add(btnAdd);

		panelCustomer.setVisible(false);
	}
	
	private void setTableCustomer(JTable jTable, ArrayList<ModelLayer.Contact> contacts) {
		DefaultTableModel model = new DefaultTableModel();
		jTable.setModel(model);
	    model.setColumnIdentifiers(new String[]{"ID", "Name", "Type", "Address", "E-mail", "Phone"});
	    String name = "";
    	String type = "";
	    for (Contact c : contacts) {
	    	if(c instanceof Private) {
	    		Private p = (Private) c;
	    		name = p.getName() + " " + p.getSurname(); 
	    		type = "private"; }
	    	else {
	    		Business b = (Business) c;
	    		name = b.getName();
	    		type = "company"; }
	        model.addRow(new String[]{Integer.toString(c.getId()), name, type, c.getAddress(), c.getEMail(), c.getPhoneNO() });
	    }
	}
	
	public void refreshTCustomer()
	{
		contacts = search.getAllCustomers();
		setTableCustomer(tableCustomer, contacts);
	}
	
	
	private void buildDelivery() 
	{
		panelDelivery.setBackground(new Color(255, 255, 204));
		panelDelivery.setBounds(0, 0, 675, 478);
		contentPane.add(panelDelivery);
		panelDelivery.setLayout(null);
		
		JLabel lblFilter = new JLabel("Filter:");
		lblFilter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFilter.setBounds(0, 0, 39, 17);
		panelDelivery.add(lblFilter);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 31, 22, 14);
		panelDelivery.add(lblId);
		
		final JTextField txtDid = new JTextField();
		txtDid.setBounds(33, 28, 86, 20);
		panelDelivery.add(txtDid);
		txtDid.setColumns(10);
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(10, 56, 109, 14);
		panelDelivery.add(lblStatus);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "Created", "In progress", "Delivered", "Canceled"}));
		comboBox.setBounds(10, 74, 109, 20);
		panelDelivery.add(comboBox);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deliveries.clear();
				if(!txtDid.getText().isEmpty())
				{	
					deliveries.add(search.getDeliveryById(Integer.parseInt(txtDid.getText())));
					
				}
				else
				{
					if(!comboBox.getSelectedItem().toString().isEmpty())
					{
						deliveries = search.getDeliveriesByStatus(comboBox.getSelectedItem().toString());
					}
					else
					{
						deliveries = search.getAllDelivery();
					}
				}
				setTableDelivery(tableDelivery, deliveries);
			}
		});
		btnSearch.setBounds(556, 74, 109, 23);
		panelDelivery.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 105, 655, 218);
		panelDelivery.add(scrollPane);
		
		deliveries = search.getAllDelivery();
		tableDelivery = new JTable();
		tableDelivery.setFillsViewportHeight(true);
		setTableDelivery(tableDelivery, deliveries);
		tableDelivery.getColumnModel().getColumn(0).setResizable(false);
		tableDelivery.getColumnModel().getColumn(1).setResizable(false);
		tableDelivery.getColumnModel().getColumn(2).setResizable(false);
		tableDelivery.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableDelivery.setBackground(Color.WHITE);
		scrollPane.getViewport().add(tableDelivery);
		
		JButton btnShow = new JButton("Show details");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableDelivery.getSelectedRow();
                if(SelRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                        int idSelected = (Integer) tableDelivery.getValueAt(SelRowIndex, 0);
                        		delivery = new Delivery(false, idSelected);
                        		delivery.setVisible(true);
                }
			}
		});
		btnShow.setBounds(10, 334, 109, 23);
		panelDelivery.add(btnShow);
				
		final int nextIndex = create.getNextDeliveryId();
		JButton btnCreateNew = new JButton("New");
		btnCreateNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delivery = new Delivery(true, nextIndex);
				delivery.setVisible(true);
			}
		});
		btnCreateNew.setBounds(129, 334, 109, 23);
		panelDelivery.add(btnCreateNew);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selRowIndex = tableDelivery.getSelectedRow();
				if(selRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                	DataHolder.getInstance().setDeliveryHolder(search.getDeliveryById(Integer.parseInt(tableDelivery.getValueAt(selRowIndex, 0).toString())));
                	dispose();
                }
			}
		});
		btnAdd.setBounds(556, 334, 109, 23);
		panelDelivery.add(btnAdd);
		
		panelDelivery.setVisible(false);	
	}
	
	private void setTableDelivery(JTable jTable, ArrayList<ModelLayer.Delivery> deliveries) {
	    DefaultTableModel model = new DefaultTableModel();
	    model.setColumnIdentifiers(new String[]{"ID", "Date", "Status"});
	    for (ModelLayer.Delivery d : deliveries) {
	        model.addRow(new String[]{Integer.toString(d.getDeliveryId()), DateFormat.getInstance().format(d.getDeliveryDate()).substring(0,8), d.getDeliveryState()});
	    }
	    jTable.setModel(model);
	}
	
	public void refreshTDelivery()
	{
		deliveries = search.getAllDelivery();
		setTableDelivery(tableDelivery, deliveries);
	}
	
	private void buildEmployee() 
	{
		panelEmployee.setBackground(new Color(255, 255, 204));
		panelEmployee.setBounds(0, 0, 675, 478);
		contentPane.add(panelEmployee);
		panelEmployee.setLayout(null);
		
		JLabel lblFilter = new JLabel("Filter:");
		lblFilter.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFilter.setBounds(0, 0, 39, 17);
		panelEmployee.add(lblFilter);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(129, 31, 62, 14);
		panelEmployee.add(lblName);
		
		final JTextField txtName = new JTextField();
		txtName.setBounds(190, 28, 86, 20);
		panelEmployee.add(txtName);
		txtName.setColumns(10);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 31, 22, 14);
		panelEmployee.add(lblId);
		
		final JTextField txtEid = new JTextField();
		txtEid.setBounds(33, 28, 86, 20);
		panelEmployee.add(txtEid);
		txtEid.setColumns(10);
		
		JLabel lblPayment = new JLabel("Role:");
		lblPayment.setBounds(10, 56, 109, 14);
		panelEmployee.add(lblPayment);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "Manager", "Warehouse", "Office", "Delivery"}));
		comboBox.setBounds(10, 74, 109, 20);
		panelEmployee.add(comboBox);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				employees.clear();
				if(!txtEid.getText().isEmpty())
				{	
					employees.add(search.getEmployeeById(Integer.parseInt(txtEid.getText())));
				}
				else
				{
					if(!txtName.getText().isEmpty())
					{
						if(!comboBox.getSelectedItem().toString().isEmpty())
						{
							employees = search.getEmployeeByNameAndRole(txtName.getText(), comboBox.getSelectedItem().toString());
						}
						else
						{
							employees = search.getEmployeesLikeName(txtName.getText());
						}
					}
					else
					{
						if(!comboBox.getSelectedItem().toString().isEmpty())
						{
							employees = search.getEmployeesByRole(comboBox.getSelectedItem().toString());
						}
						else
						{
							employees = search.getAllEmployees();
						}
					}
				}

				setTableEmployee(tableEmployee, employees);
			}
		});
		btnSearch.setBounds(556, 74, 109, 23);
		panelEmployee.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 105, 655, 218);
		panelEmployee.add(scrollPane);
		
		employees = search.getAllEmployees();
		tableEmployee = new JTable();
		tableEmployee.setFillsViewportHeight(true);
		setTableEmployee(tableEmployee, employees);
		tableEmployee.getColumnModel().getColumn(0).setResizable(false);
		tableEmployee.getColumnModel().getColumn(1).setResizable(false);
		tableEmployee.getColumnModel().getColumn(2).setResizable(false);
		tableEmployee.getColumnModel().getColumn(3).setResizable(false);
		tableEmployee.getColumnModel().getColumn(4).setResizable(false);
		tableEmployee.getColumnModel().getColumn(5).setResizable(false);
		tableEmployee.getColumnModel().getColumn(6).setResizable(false);
		tableEmployee.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableEmployee.setBackground(Color.WHITE);
		scrollPane.getViewport().add(tableEmployee);
		
		JButton btnShow = new JButton("Show details");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableEmployee.getSelectedRow();
                if(SelRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                        int idSelected = (Integer) tableEmployee.getValueAt(SelRowIndex, 0);
                        		employee = new GUILayer.Employee(false, false, idSelected);
                        		employee.setVisible(true);
                }
			}
		});
		btnShow.setBounds(10, 334, 109, 23);
		panelEmployee.add(btnShow);
		
		final int id = create.getNextContactId();
		JButton btnCreateNew = new JButton("New");
		btnCreateNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				employee = new GUILayer.Employee(true, false, id);
				employee.setVisible(true);
			}
		});
		btnCreateNew.setBounds(129, 129, 109, 23);
		panelEmployee.add(btnCreateNew);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selRowIndex = tableEmployee.getSelectedRow();
				if(selRowIndex==-1)
                {
                        JOptionPane.showMessageDialog(null,
                                    "You have to chose one from the list",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                	DataHolder.getInstance().setEmployeeHolder(search.getEmployeeById(Integer.parseInt(tableEmployee.getValueAt(selRowIndex,0).toString())));
                	dispose();
                }
			}
		});
		btnAdd.setBounds(556, 334, 109, 23);
		panelEmployee.add(btnAdd);
		
		panelEmployee.setVisible(false);
	}
	
	private void setTableEmployee(JTable jTable, ArrayList<ModelLayer.Employee> employees) {
	    DefaultTableModel model = new DefaultTableModel();
	    jTable.setModel(model);
	    model.setColumnIdentifiers(new String[]{"ID", "Name", "Address", "E-mail", "Telephone", "Role", "Salary"});
	    for (ModelLayer.Employee e : employees) {
	    	String name = e.getName() + " " + e.getSurname();
	        model.addRow(new String[]{Integer.toString(e.getId()), name, e.getAddress(), e.getEMail(), e.getPhoneNO(), e.getRole(), Double.toString(e.getSalaryPerHour())});
	    }
	}
	
	public void refreshTEmployee()
	{
		employees = search.getAllEmployees();
		setTableEmployee(tableEmployee, employees);
	}
}
